const Koa = require("koa");
const Router = require("koa-router");
const views = require("koa-views");
// https://www.npmjs.com/
const static = require("koa-static");
let app = new Koa();
let router = new Router();

const server = require("http").createServer(app.callback());
const io =require("socket.io")(server);//监听websocket链接
// console.log(__dirname);
// console.log(__filename);
io.on("connection",socket=>{
    socket.on("coord",function(res){
      //  console.log(res);
       socket.emit("send",res);
       socket.broadcast.emit("send",res)//发送事件给除了自己之外 
    })
})
io.on("connection",socket=>{
   socket.on("sendXY",function(res){
      console.log(res);
      socket.emit("toxy",res);
      socket.broadcast.emit("toxy",res)//发送事件给除了自己之外 
   })
})
app.use(views(__dirname+"/views"));
app.use(static(__dirname+"/static"));
router.get("/index",async ctx=>{
   await ctx.render("controlblock.html");
});
router.get("/index2",async ctx=>{
   await ctx.render("controlblock2.html");
});
router.get("/product",async ctx=>{
     ctx.body=[{user:"zhangsan"}];
});

app.use(router.routes());
server.listen(8989);